package com.situ.mall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.mall.common.util.Result;
import com.situ.mall.pojo.entity.product.NewProduct;
import com.situ.mall.pojo.entity.product.Product;
import com.situ.mall.product.pojo.query.NewProductQuery;
import com.situ.mall.product.service.INewProductService;
import com.situ.mall.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sun Huijuan
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/product/new-product")
public class NewProductController {
	@Autowired
	private INewProductService newProductService;
	
	@Autowired
	private IProductService productService;
	
	@GetMapping("/list")
	public Result<Map<String,Object>> list(NewProductQuery newproductQuery) {
		IPage<NewProduct> page = new Page<>(newproductQuery.getPage(), newproductQuery.getLimit());
		QueryWrapper<NewProduct> queryWrapper = new QueryWrapper<>();
		queryWrapper.like(!ObjectUtils.isEmpty(newproductQuery.getName()), "name", newproductQuery.getName());
		queryWrapper.between(!ObjectUtils.isEmpty(newproductQuery.getBeginUpdateTime())&&!ObjectUtils.isEmpty(newproductQuery.getEndUpdateTime()),
				"update_time",newproductQuery.getBeginUpdateTime(),newproductQuery.getEndUpdateTime());
		newProductService.page(page, queryWrapper);
		Map<String, Object> map = new HashMap<>();
		map.put("total", page.getTotal());
		map.put("records", page.getRecords());
		return Result.ok(map);
	}
	
	@GetMapping("/selectAll")
	public Result<List<NewProduct>> selectAllNewProduct(){
		QueryWrapper<NewProduct> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status",1);
		List<NewProduct> list=newProductService.list(queryWrapper);
		return Result.ok(list);
	}
	
	@DeleteMapping("/deleteAll/{ids}")
	public Result<Object> deleteAll(@PathVariable Integer[] ids) {
		newProductService.removeByIds(Arrays.asList(ids));
		return Result.ok("删除成功！");
	}
	
	@DeleteMapping("/deleteById/{id}")
	public Result deleteById(@PathVariable Integer id) {
		newProductService.removeById(id);
		return Result.ok("删除成功！");
	}
	
	@PostMapping("/add")
	public Result add(@RequestBody NewProduct newProduct) {
		if(newProduct.getName()!=null&&newProduct.getName()!=""){
			QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("name", newProduct.getName());
			Product product=productService.getOne(queryWrapper);
			if(product!=null){
				newProduct.setProductId(product.getId());
			}else {
				return Result.error("产品不存在无法添加！");
			}
		}
		newProductService.save(newProduct);
		return Result.ok("添加成功！");
	}
	
	@GetMapping("/selectById/{id}")
	public Result<NewProduct> selectById(@PathVariable Long id) {
		NewProduct newProduct=newProductService.getById(id);
		return Result.ok(newProduct);
	}
	
	@PutMapping("/update")
	public Result update(@RequestBody NewProduct newProduct) {
		if(newProduct.getName()!=null&&newProduct.getName()!=""){
			QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("name", newProduct.getName());
			Product product=productService.getOne(queryWrapper);
			if(product!=null){
				newProduct.setProductId(product.getId());
			}else {
				return Result.error("产品不存在无法修改！");
			}
		}
		newProductService.saveOrUpdate(newProduct);
		return Result.ok("修改成功！");
	}
	
	@PutMapping("/updateStatus/{id}/{status}")
	public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
		UpdateWrapper<NewProduct> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id",id).set("status",status);
		newProductService.update(updateWrapper);
		return Result.ok("修改成功！");
	}
	
}

